//
//  TTDianpuGoodsViewCell.m
//  TaoTuo
//
//  Created by admin on 2019/11/11.
//  Copyright © 2019 admin. All rights reserved.
//

#import "TTDianpuGoodsViewCell.h"
#import "TTOrderShopModel.h"
#import "TTUpDownButton.h"
#import <UIImageView+WebCache.h>

@interface TTDianpuGoodsViewCell ()

@property (nonatomic, strong) UIButton *leftBtn;
@property (nonatomic, strong) UIImageView *leftImageView;
@property (nonatomic, strong) UIImageView *sellOutImageView;
@property (nonatomic, strong) UILabel *leftLabel;
@property (nonatomic, strong) UILabel *titleLabel;
@property (nonatomic, strong) UILabel *contentLabel;
@property (nonatomic, strong) TTUpDownButton *locationBtn;
@property (nonatomic, strong) UILabel *bottomLabel; // 定制才有，做工：颜色：
@property (nonatomic, strong) UILabel *productionTimeLabel; // 生产周期
@property (nonatomic, strong) UILabel *priceLabel;  // 价格
@property (nonatomic, strong) UIView *lineView;


@end

@implementation TTDianpuGoodsViewCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        
        self.backgroundColor = UIColorHex(F1F5F6);
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        self.subject = [RACSubject subject];
        [self setupSubviews];
        
    }
    return self;
}

- (void)setModel:(TTOrderShopModel *)model
{
    _model = model;
    
    self.leftBtn.selected = model.isSelected;
    self.leftBtn.enabled = model.SysStatus.intValue == 1;
    
    NSString *imageUrlString = [model.GoodsImage urlEncode];
    [self.leftImageView sd_setImageWithURL:[NSURL URLWithString:imageUrlString] placeholderImage:[UIImage imageNamed:@"f_placeholder_img"] options:SDWebImageRetryFailed];
    self.leftLabel.text = model.ProductText;
    
    
    self.titleLabel.text = model.GoodsTitle;
    NSData *data = [model.GoodsDescribe dataUsingEncoding:NSUTF8StringEncoding];
    NSArray *describe = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
    self.contentLabel.text = [describe componentsJoinedByString:@"\n"];
    
    self.bottomLabel.hidden = model.GoodsType != 2;
    self.productionTimeLabel.hidden = model.GoodsType != 2;
    
    NSString *price = [NSString stringWithFormat:@"￥%@", model.PlusShopGoodsPrice];
    NSString *str = [NSString stringWithFormat:@"%@%@%@", @"", price, @""];
    NSMutableAttributedString *attr = [[NSMutableAttributedString alloc] initWithString:str];
    [attr addAttributes:@{NSFontAttributeName : [UIFont systemFontOfSize:15], NSForegroundColorAttributeName : UIColorHex(FF1F1F)} range:[str rangeOfString:price]];
    self.priceLabel.attributedText = attr;
    
    [self.locationBtn setTitle:model.GoodsLocation forState:UIControlStateNormal];
    
    NSString *sellImageName = @"";
    if (model.SysStatus.intValue == 2) {
        sellImageName = @"shopcartsellout";
    } else if (model.SysStatus.intValue == 3) {
        sellImageName = @"xiajia";
    }
    self.sellOutImageView.image = [UIImage imageNamed:sellImageName];
    self.sellOutImageView.hidden = model.SysStatus.intValue == 1;
    
}

- (void)leftBtnClick:(UIButton *)sender
{
    sender.selected = !sender.selected;
    self.model.isSelected = sender.selected;
    [self.subject sendNext:self.model];
}

- (void)setupSubviews
{
    _leftBtn = [UIButton createButtonWithNormalImage:[UIImage imageNamed:@"shopcart_unsel"] selectedImage:[UIImage imageNamed:@"shopcartlist_sel"] target:self action:@selector(leftBtnClick:)];
    [_leftBtn setImage:[UIImage imageNamed:@"shopcart_unable"] forState:UIControlStateDisabled];
    _leftBtn.imageView.contentMode = UIViewContentModeCenter;
    [self.contentView addSubview:_leftBtn];
    [_leftBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.bottom.equalTo(self.contentView);
        make.width.mas_equalTo(30 * kScale);
    }];
    
    _leftImageView = [[UIImageView alloc] init];
    _leftImageView.layer.cornerRadius = 10;
    _leftImageView.layer.masksToBounds = YES;
    _leftImageView.contentMode = UIViewContentModeScaleAspectFit;
    [self.contentView addSubview:_leftImageView];
    [_leftImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.leftBtn.mas_right);
        make.centerY.equalTo(self.contentView).mas_offset(-12);
        make.width.height.mas_equalTo(65 * kScale);
    }];
    
    _sellOutImageView = [[UIImageView alloc] init];
    _sellOutImageView.contentMode = UIViewContentModeCenter;
    [self.contentView addSubview:_sellOutImageView];
    [_sellOutImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.leftImageView);
    }];
    
    _leftLabel = [UILabel labelWithTitle:@"" color:UIColorHex(999999) fontSize:9];
    [self.contentView addSubview:_leftLabel];
    [_leftLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.leftImageView.mas_bottom).mas_offset(5);
        make.centerX.equalTo(self.leftImageView);
    }];
    
    _titleLabel = [UILabel labelWithTitle:@"" color:[UIColor blackColor] fontSize:15];
    [self.contentView addSubview:_titleLabel];
    [_titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(8);
        make.left.equalTo(self.leftImageView.mas_right).mas_offset(10);
    }];
    
    //    SCREEN_WIDTH - 10 - 10 - 65 * kScale - 10 - 10
    _contentLabel = [UILabel labelWithTitle:@"" color:UIColorHex(333333) fontSize:12];
    _contentLabel.numberOfLines = 0;
    [self.contentView addSubview:_contentLabel];
    [_contentLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.titleLabel.mas_bottom).mas_offset(5);
        make.left.equalTo(self.titleLabel);
        make.right.equalTo(self.contentView).mas_offset(-10);
    }];
    
    _locationBtn = [TTUpDownButton buttonWithType:UIButtonTypeCustom];
    [_locationBtn setTitleColor:UIColorHex(DEDEDE) forState:UIControlStateNormal];
    [_locationBtn setImage:[UIImage imageNamed:@"shopcart_dizhi"] forState:UIControlStateNormal];
    _locationBtn.titleLabel.font = [UIFont systemFontOfSize:9];
//    _locationBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight;
    [self.contentView addSubview:_locationBtn];
    [_locationBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.titleLabel);
        make.right.equalTo(self.contentView);
        make.width.mas_equalTo(40);
        make.height.mas_equalTo(30);
    }];
    
    // 生产周期
    _productionTimeLabel = [UILabel labelWithTitle:@"" color:kMainColor fontSize:12];
    [self.contentView addSubview:_productionTimeLabel];
    [_productionTimeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.contentLabel.mas_bottom).mas_offset(5);
        make.right.equalTo(self.contentView).mas_offset(-10);
    }];
    
    // 价格
    _priceLabel = [UILabel labelWithTitle:@"" color:UIColorHex(DEDEDE) fontSize:9];
    [self.contentView addSubview:_priceLabel];
    [_priceLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.productionTimeLabel.mas_bottom).mas_offset(5);
        make.right.equalTo(self.productionTimeLabel);
    }];
    
    // 做工、颜色
    _bottomLabel = [UILabel labelWithTitle:@"" color:UIColorHex(333333) fontSize:12];
    [self.contentView addSubview:_bottomLabel];
    [_bottomLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.priceLabel);
        make.left.equalTo(self.titleLabel);
    }];
    
    _lineView = [[UIView alloc] init];
    _lineView.backgroundColor = [UIColor whiteColor];
    [self.contentView addSubview:_lineView];
    [_lineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(10);
        make.right.equalTo(self.contentView).mas_offset(-10);
        make.bottom.equalTo(self.contentView).mas_offset(1);
        make.height.mas_equalTo(1);
    }];
    
    
}


@end
